<template>
    <div class="category-page-container">
      <!-- 页面标题部分 -->
      <div class="banner">
        <div class="banner-content">
          <h1>京剧行当</h1>
          <p>探索生旦净丑四大行当的艺术特色</p>
        </div>
      </div>
      
      <!-- 行当分类导航 -->
      <div class="main-category-nav">
        <el-tabs v-model="activeMainCategory" type="card" @tab-click="handleMainCategoryChange">
          <el-tab-pane label="全部行当" name="all"></el-tab-pane>
          <el-tab-pane label="生角" name="生">
            <span slot="label"><i class="main-category-icon">生</i>生角</span>
          </el-tab-pane>
          <el-tab-pane label="旦角" name="旦">
            <span slot="label"><i class="main-category-icon">旦</i>旦角</span>
          </el-tab-pane>
          <el-tab-pane label="净角" name="净">
            <span slot="label"><i class="main-category-icon">净</i>净角</span>
          </el-tab-pane>
          <el-tab-pane label="丑角" name="丑">
            <span slot="label"><i class="main-category-icon">丑</i>丑角</span>
          </el-tab-pane>
        </el-tabs>
      </div>
      
      <!-- 主要内容区域 -->
      <div class="content-section">
        <!-- 行当介绍 -->
        <div class="main-category-intro" v-if="activeMainCategory !== 'all'">
          <div class="intro-content">
            <h2>{{ getMainCategoryInfo(activeMainCategory).title }}</h2>
            <p>{{ getMainCategoryInfo(activeMainCategory).description }}</p>
          </div>
        </div>
        
        <!-- 行当卡片列表 -->
        <div class="category-grid" v-loading="loading">
          <el-card v-for="category in filteredCategories" :key="category.categoriesId" 
            class="category-card" shadow="hover" @click.native="viewCategoryDetail(category)">
            <div class="category-card-header">
              <div class="category-tag" :class="'tag-' + getMainCategoryClass(category.type)">
                {{ category.type }}
              </div>
              <h3 class="category-name">{{ category.categoriesName }}</h3>
            </div>
            <div class="category-card-content">
              <p class="category-description">{{ truncateText(category.description || '暂无描述', 80) }}</p>
            </div>
            <div class="category-card-footer">
              <span class="create-time">建立于: {{ category.createYear }}</span>
              <el-button type="text" size="small" @click.stop="viewRelatedArtists(category)">
                查看相关艺术家 <i class="el-icon-arrow-right"></i>
              </el-button>
            </div>
          </el-card>
        </div>
        
        <!-- 空状态 -->
        <div class="empty-state" v-if="filteredCategories.length === 0 && !loading">
          <img src="@/assets/opera_logo.png" alt="暂无数据">
          <p>暂无相关行当数据</p>
        </div>
      </div>
      
      <!-- 详情弹窗 -->
      <el-dialog title="行当详情" :visible.sync="detailVisible" width="70%" class="category-detail-dialog">
        <div v-if="currentCategory" class="category-detail">
          <div class="detail-header">
            <div class="detail-title">
              <span class="main-category-badge" :class="'badge-' + getMainCategoryClass(currentCategory.type)">
                {{ currentCategory.type }}角
              </span>
              <h2>{{ currentCategory.categoriesName }}</h2>
            </div>
            <div class="detail-meta">
              <span>成立时间: {{ currentCategory.createYear }}</span>
            </div>
          </div>
          
          <el-divider content-position="left">基本介绍</el-divider>
          <p class="detail-description">{{ currentCategory.description || '暂无详细介绍' }}</p>
          
          <el-divider content-position="left">相关艺术家</el-divider>
          <div class="related-artists">
            <div v-if="relatedArtists.length > 0" class="artist-list">
              <div v-for="artist in relatedArtists" :key="artist.artistId" class="artist-item" 
                @click="goToArtistDetail(artist.artistId)">
                <div class="artist-avatar">
                  <img :src="artist.avatar || require('@/assets/history/opera_mlf.jpg')" :alt="artist.artistName">
                </div>
                <div class="artist-name">{{ artist.artistName }}</div>
              </div>
            </div>
            <el-empty v-else description="暂无相关艺术家" :image-size="120"></el-empty>
          </div>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import service from '@/utils/request'
  
  export default {
    name: 'CategoryPage',
    data() {
      return {
        activeMainCategory: 'all',
        categories: [],
        loading: false,
        detailVisible: false,
        currentCategory: null,
        relatedArtists: [],
        
        // 四大行当基本资料
        mainCategoryInfo: {
          '生': {
            title: '生角 - 男性角色的总称',
            description: '生角是京剧中男性角色的总称，根据年龄、性格特点等不同，又分为老生、小生、武生等。老生扮演中老年男性，以唱工见长；小生扮演年轻男性，多为文质彬彬的书生形象；武生则以武打技巧见长，表演气势豪迈。',
          },
          '旦': {
            title: '旦角 - 女性角色的总称',
            description: '旦角是京剧中女性角色的总称，包括青衣、花旦、武旦、老旦等。青衣扮演端庄典雅的成年女性；花旦扮演活泼俏丽的年轻女子；武旦以武打技巧见长；老旦则表演年长的女性角色。梅兰芳是最著名的旦角表演艺术家之一。',
           
          },
          '净': {
            title: '净角 - 性格鲜明的男性角色',
            description: '净角又称"花脸"，主要扮演性格鲜明、特点突出的男性角色，如忠臣、奸臣、猛将等。其特点是脸上画有独特的脸谱，行当内又分为铜锤花脸、架子花脸、武二花脸等。净角表演注重气势，唱腔宽厚有力。',
           
          },
          '丑': {
            title: '丑角 - 喜剧角色',
            description: '丑角是京剧中的喜剧角色，脸上画有白色鼻梁，表演诙谐幽默。丑角分为文丑和武丑，文丑多扮演滑稽诙谐的市井小民，武丑则多扮演勇武之将。丑角表演夸张生动，常运用科诨、身段等手段制造笑点。',
         
          }
        }
      }
    },
    computed: {
      filteredCategories() {
        if (this.activeMainCategory === 'all') {
          return this.categories;
        }
        return this.categories.filter(category => 
          category.type === this.activeMainCategory
        );
      }
    },
    mounted() {
      document.title = '京剧苑 - 京剧行当';
      this.loadCategories();
    },
    methods: {
      // 加载所有行当数据
      async loadCategories() {
        this.loading = true;
        try {
          const res = await service.get('/api/content/category/getCategoryList', {
            params: {
              pageNum: 1,
              pageSize: 100 // 加载所有行当
            }
          });
          
          if (res.code === 200) {
            this.categories = res.data.list;
            console.log(this.categories);   
          } else {
            this.$message.error('获取行当数据失败: ' + res.message);
          }
        } catch (error) {
          console.error('获取行当数据失败:', error);
          this.$message.error('获取行当数据失败');
        } finally {
          this.loading = false;
        }
      },
      
      // 获取行当大类信息
      getMainCategoryInfo(mainCategory) {
        return this.mainCategoryInfo[mainCategory] || {
          title: '京剧行当',
          description: '京剧行当是根据角色的性别、年龄、性格等特点划分的表演行当，主要分为生、旦、净、丑四大类。',
        };
      },
      
      // 获取行当主类对应的样式名
      getMainCategoryClass(mainCategory) {
        const map = {
          '生': 'sheng',
          '旦': 'dan',
          '净': 'jing',
          '丑': 'chou'
        };
        return map[mainCategory] || 'default';
      },
      
      // 处理主行当切换
      handleMainCategoryChange(tab) {
        console.log('切换到:', tab.name);
      },
      
      // 截断过长文本
      truncateText(text, maxLength) {
        if (!text) return '';
        return text.length > maxLength ? text.substring(0, maxLength) + '...' : text;
      },
      
      // 格式化日期
      formatDate(dateString) {
        if (!dateString) return '未知日期';
        const date = new Date(dateString);
        return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
      },
      
      // 查看行当详情
      viewCategoryDetail(category) {
        this.currentCategory = category;
        this.loadRelatedArtists(category.categoriesId);
        this.detailVisible = true;
      },
      
      // 查看相关艺术家
      viewRelatedArtists(category) {
        this.$router.push({
          path: '/artists',
          query: { categoryId: category.categoriesId }
        });
      },
      
      // 加载相关艺术家
      async loadRelatedArtists(categoryId) {
        try {
          const res = await service.get(`/api/content/artist/getArtistByCategoryId/${categoryId}`);
          if (res.code === 200) {
            this.relatedArtists = res.data || [];
          } else {
            this.relatedArtists = [];
          }
        } catch (error) {
          console.error('获取相关艺术家失败:', error);
          this.relatedArtists = [];
        }
      },
      
      // 跳转到艺术家详情页
      goToArtistDetail(artistId) {
        this.$router.push({
          path: `/artist/${artistId}`
        });
      }
    }
  }
  </script>
  
  <style scoped>
  .category-page-container {
    min-height: 100vh;
    padding-bottom: 50px;
    background-color: #f9f5f0;
  }
  
  .banner {
    height: 500px;
    background-image: url('../assets/opera_category.png');
    background-size: cover;
    background-position: center;
    position: relative;
    margin-bottom: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .banner::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(to bottom, rgba(139, 0, 0, 0.7), rgba(0, 0, 0, 0.5));
  }
  
  .banner-content {
    position: relative;
    z-index: 1;
    text-align: center;
    color: #fff;
  }
  
  .banner-content h1 {
    font-size: 4rem;
    font-family: 'STXingkai', 'STZhongsong', serif;
    margin-bottom: 15px;
    color: #FFD700;
    text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.6);
    letter-spacing: 10px;
  }
  
  .banner-content p {
    font-size: 1.4rem;
    max-width: 600px;
    margin: 0 auto;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
    letter-spacing: 3px;
  }
  
  .main-category-nav {
    padding: 20px 5%;
    background-color: #fff;
    margin-bottom: 30px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    max-width: 1200px;
    margin-left: auto;
    margin-right: auto;
  }
  
  .main-category-icon {
    display: inline-block;
    width: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border-radius: 50%;
    background-color: #8B0000;
    color: white;
    font-weight: bold;
    margin-right: 5px;
    font-style: normal;
  }
  
  .content-section {
    padding: 30px 8%;
    max-width: 1200px;
    margin: 0 auto;
  }
  
  .main-category-intro {
    display: flex;
    gap: 30px;
    margin-bottom: 40px;
    background-color: #fff;
    border-radius: 8px;
    padding: 40px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
  
  .intro-content {
    flex: 2;
  }
  
  .intro-content h2 {
    color: #8B0000;
    font-size: 2rem;
    margin-bottom: 20px;
    font-family: 'STZhongsong', serif;
    position: relative;
    display: inline-block;
  }
  
  .intro-content h2::after {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 0;
    width: 80px;
    height: 3px;
    background-color: #8B0000;
  }
  
  .intro-content p {
    color: #333;
    line-height: 2;
    font-size: 1.1rem;
  }
  
  .intro-image {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .intro-image img {
    max-width: 100%;
    max-height: 250px;
    border-radius: 5px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
  
  .category-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 30px;
    margin-bottom: 40px;
  }
  
  .category-card {
    border-radius: 8px;
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
    height: 100%;
    display: flex;
    flex-direction: column;
    border: none;
    background-color: rgba(255, 255, 255, 0.95);
  }
  
  .category-card:hover {
    transform: translateY(-8px);
    box-shadow: 0 15px 30px rgba(139, 0, 0, 0.15);
  }
  
  .category-card-header {
    padding: 15px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #f0f0f0;
  }
  
  .category-tag {
    padding: 2px 10px;
    border-radius: 4px;
    color: white;
    font-size: 14px;
    margin-right: 10px;
  }
  
  .tag-sheng {
    background-color: #409EFF;
  }
  
  .tag-dan {
    background-color: #FF66CC;
  }
  
  .tag-jing {
    background-color: #67C23A;
  }
  
  .tag-chou {
    background-color: #E6A23C;
  }
  
  .tag-default {
    background-color: #909399;
  }
  
  .category-name {
    font-size: 1.5rem;
    margin: 0;
    color: #8B0000;
    font-weight: bold;
    font-family: 'STZhongsong', serif;
  }
  
  .category-card-content {
    padding: 20px 15px;
    flex: 1;
  }
  
  .category-description {
    color: #555;
    line-height: 1.7;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
    margin: 0;
    font-size: 1rem;
  }
  
  .category-card-footer {
    padding: 15px;
    border-top: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .create-time {
    color: #777;
    font-size: 14px;
  }
  
  .category-card-footer .el-button {
    color: #8B0000;
    font-weight: bold;
  }
  
  .empty-state {
    text-align: center;
    padding: 50px 0;
    color: #999;
    background: #f9f9f9;
    border-radius: 8px;
    margin: 20px 0;
  }
  
  .empty-state img {
    width: 150px;
    margin-bottom: 20px;
    opacity: 0.5;
  }
  
  /* 详情弹窗样式 */
  .category-detail {
    padding: 20px;
  }
  
  .detail-header {
    margin-bottom: 30px;
  }
  
  .detail-title {
    display: flex;
    align-items: center;
  }
  
  .main-category-badge {
    display: inline-block;
    padding: 5px 12px;
    border-radius: 20px;
    color: white;
    font-size: 14px;
    margin-right: 15px;
  }
  
  .badge-sheng {
    background-color: #409EFF;
  }
  
  .badge-dan {
    background-color: #FF66CC;
  }
  
  .badge-jing {
    background-color: #67C23A;
  }
  
  .badge-chou {
    background-color: #E6A23C;
  }
  
  .badge-default {
    background-color: #909399;
  }
  
  .detail-title h2 {
    margin: 0;
    font-size: 1.8rem;
    color: #8B0000;
    font-family: 'STZhongsong', serif;
  }
  
  .detail-description, .detail-features {
    color: #333;
    line-height: 2;
    font-size: 1.1rem;
    margin-bottom: 30px;
  }
  
  .related-artists {
    margin-top: 30px;
  }
  
  .artist-list {
    display: flex;
    flex-wrap: wrap;
    gap: 25px;
  }
  
  .artist-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 120px;
    cursor: pointer;
    transition: all 0.3s;
  }
  
  .artist-item:hover {
    transform: translateY(-5px);
  }
  
  .artist-avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    overflow: hidden;
    margin-bottom: 10px;
    border: 2px solid #eee;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  }
  
  .artist-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s;
  }
  
  .artist-item:hover .artist-avatar img {
    transform: scale(1.05);
  }
  
  .artist-name {
    font-size: 1rem;
    color: #333;
    text-align: center;
  }
  
  .detail-meta {
    display: flex;
    margin-top: 10px;
    color: #777;
    font-size: 1rem;
  }
  
  .detail-meta .divider {
    margin: 0 10px;
  }
  
  /* 响应式样式调整 */
  @media (max-width: 1200px) {
    .category-grid {
      grid-template-columns: repeat(2, 1fr);
    }
    
    .main-category-intro {
      flex-direction: column;
    }
    
    .intro-image {
      justify-content: flex-start;
    }
  }
  
  @media (max-width: 768px) {
    .banner-content h1 {
      font-size: 2.5rem;
      letter-spacing: 5px;
    }
    
    .banner-content p {
      font-size: 1.2rem;
      letter-spacing: 1px;
    }
    
    .banner {
      height: 240px;
    }
    
    .intro-content h2 {
      font-size: 1.8rem;
    }
    
    .intro-content p {
      font-size: 1rem;
    }
    
    .category-name {
      font-size: 1.3rem;
    }
    
    .category-grid {
      grid-template-columns: 1fr;
    }
  }
  
  @media (max-width: 480px) {
    .banner-content h1 {
      font-size: 2rem;
      letter-spacing: 3px;
    }
    
    .banner-content p {
      font-size: 1rem;
    }
    
    .artist-item {
      width: 90px;
    }
    
    .artist-avatar {
      width: 70px;
      height: 70px;
    }
    
    .artist-name {
      font-size: 0.9rem;
    }
  }
  </style>